package com.zy.garbage.service.impl;

import com.zy.garbage.entity.User;
import com.zy.garbage.enums.SessionEnum;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.form.AccountCheckForm;
import com.zy.garbage.form.AccountForm;
import com.zy.garbage.form.UpdateAccountForm;
import com.zy.garbage.repository.UserRepository;
import com.zy.garbage.service.AccountService;
import com.zy.garbage.util.JwtUtil;
import com.zy.garbage.util.MD5Util;
import com.zy.garbage.util.ResultUtil;
import com.zy.garbage.vo.AccountVO;
import com.zy.garbage.form.CreateAccountForm;
import com.zy.garbage.vo.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private JwtUtil jwtUtil;


    @Override
    public AccountVO login(AccountForm accountForm, SessionEnum session) {
        User user = userRepository.findByUserPhone(accountForm.getUserPhone());
        if (user == null)
            throw new CommonException(500, "账号不存在");
//        if (!user.getUserPwd().equals(MD5Util.getMD5(accountForm.getPassword()))) {
//            throw new CommonException(500, "密码不正确");
//        }
        String code = session.getCode();
        if (!accountForm.getCode().equalsIgnoreCase(code)){
            throw new CommonException(500,"验证码不正确");
        }
        if (!user.getUserPwd().equals(accountForm.getPassword())) {
            throw new CommonException(500, "密码不正确");
        }


        Map<String, String> map = new HashMap<String, String>();
        map.put("role", user.getUserRole());
        map.put("perms", user.getUserPerms());
        String jwt = jwtUtil.createJwt(user.getUserPhone(), map);
        AccountVO accountVO = new AccountVO();
        accountVO.setPhone(user.getUserPhone());
        accountVO.setToken(jwt);
        accountVO.setUsername(user.getUserName());
        accountVO.setPassword(user.getUserPwd());
        accountVO.setRole(user.getUserRole());
        accountVO.setPerms(user.getUserPerms());
        accountVO.setStatus(user.getUserStatus());
        accountVO.setUserId(user.getUserId());
        return accountVO;
    }

    @Override
    public UserInfoVO findByUserPhone(String userPhone) {
        List<User> users = userRepository.findAllByUserPhone(userPhone);
        if (users.size() != 0) {
            User user = userRepository.findByUserPhone(userPhone);
            UserInfoVO userInfoVO = new UserInfoVO();
            BeanUtils.copyProperties(user, userInfoVO);
            return userInfoVO;
        }
        throw new CommonException(500, "该用户不存在");
    }

    @Override
    public void save(CreateAccountForm createAccountForm) {
        List<User> users = userRepository.findAllByUserPhone(createAccountForm.getUserPhone());
        if (users.size() == 0) {
            User user = new User();
//            String password = MD5Util.getMD5(createAccountForm.getUserPwd());
            String password = createAccountForm.getUserPwd();
            createAccountForm.setUserPwd(password);
            BeanUtils.copyProperties(createAccountForm, user);
            userRepository.save(user);
            return;
        }
        throw new CommonException(500, "该用户已存在");


    }

    @Override
    public void update(UpdateAccountForm updateAccountForm) {

//        String userStatus = updateAccountForm.getUserStatus();
//        if (!(userStatus.equals("0")||userStatus.equals("1")||userStatus.equals("2"))){
//            throw new CommonException(500,"状态码错误");
//        }
        if (userRepository.findById(updateAccountForm.getUserId()).isPresent()) {
            User user = userRepository.findById(updateAccountForm.getUserId()).get();
            BeanUtils.copyProperties(updateAccountForm, user);
            userRepository.save(user);
            return;
        }
        throw new CommonException(500, "该用户不存在");
    }

    @Override
    public void updatePwd(AccountForm accountForm) {
        String userPhone = accountForm.getUserPhone();
        String password = accountForm.getPassword();
        String newPwd = accountForm.getNewPwd();
        if (userRepository.findByUserPhone(userPhone) == null) {
            throw new CommonException(500, "找不到该用户");
        } else {
            User user = userRepository.findByUserPhone(userPhone);
//            if (MD5Util.getMD5(password).equals(user.getUserPwd())) {
//                user.setUserPwd(MD5Util.getMD5(newPwd));
//                userRepository.save(user);
//                return;
//            }
            if (password.equals(user.getUserPwd())) {
                user.setUserPwd(newPwd);
                userRepository.save(user);
                return;
            }
            throw new CommonException(500, "密码错误");
        }

    }

    @Override
    public List<UserInfoVO> findAll() {
        List<User> userList = userRepository.findAll();
        List<UserInfoVO> userInfoVOList = new ArrayList<>();
        for (User user : userList) {
            UserInfoVO userInfoVO = new UserInfoVO();

            BeanUtils.copyProperties(user,userInfoVO);
            userInfoVOList.add(userInfoVO);
        }
        return userInfoVOList;
    }

    @Override
    public void setPwd(AccountForm accountForm) {
        String userPhone = accountForm.getUserPhone();
        String password = accountForm.getPassword();
        if (userRepository.findByUserPhone(userPhone) == null) {
            throw new CommonException(500,"找不到该用户");
        } else {
            User user = userRepository.findByUserPhone(userPhone);
//            user.setUserPwd(MD5Util.getMD5(password));
            user.setUserPwd(password);
            userRepository.save(user);
        }
    }

    @Override
    public void check(AccountCheckForm accountCheckForm) {
        Integer userId = accountCheckForm.getUserId();
        if (!userRepository.findById(userId).isPresent()){
            throw new CommonException(500,"用户不存在");
        }
        User user = userRepository.findById(userId).get();
        String userStatus = accountCheckForm.getUserStatus();
        if (!(userStatus.equals("0")||userStatus.equals("1")||userStatus.equals("2"))){
            throw new CommonException(500,"状态码错误");
        }
        user.setUserStatus(userStatus);
        userRepository.save(user);
    }

}
